//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Managers;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Product manufacturer mapping manager
    /// </summary>
    public class ProductManufacturerManager
    {
        #region Constants
        private const string PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY = "Nop.productmanufacturer.allbymanufacturerid-{0}-{1}";
        private const string PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY = "Nop.productmanufacturer.allbyproductid-{0}-{1}";
        private const string PRODUCTMANUFACTURERS_BY_ID_KEY = "Nop.productmanufacturer.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Deletes a product manufacturer mapping
        /// </summary>
        /// <param name="ProductManufacturerID">Product manufacturer mapping identifer</param>
        public static void DeleteProductManufacturer(int ProductManufacturerID)
        {
            ProductManufacturerDBManager.DeleteProductManufacturer(ProductManufacturerID);

            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY);
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY);
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_BY_ID_KEY);
            }
        }

        /// <summary>
        /// Gets product category manufacturer collection
        /// </summary>
        /// <param name="ManufacturerID">Manufacturer identifier</param>
        /// <returns>Product category manufacturer collection</returns>
        public static ProductManufacturerCollection GetProductManufacturersByManufacturerID(int ManufacturerID)
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY, showHidden, ManufacturerID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductManufacturerCollection)obj2;
            }

            ProductManufacturerCollection productManufacturerCollection = ProductManufacturerDBManager.GetProductManufacturersByManufacturerID(ManufacturerID, showHidden);
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productManufacturerCollection);
            }
            return productManufacturerCollection;
        }

        /// <summary>
        /// Gets a product manufacturer mapping collection
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <returns>Product manufacturer mapping collection</returns>
        public static ProductManufacturerCollection GetProductManufacturersByProductID(int ProductID)
        {
            bool showHidden = NopContext.Current.IsAdmin;
            string key = string.Format(PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY, showHidden, ProductID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductManufacturerCollection)obj2;
            }

            ProductManufacturerCollection productManufacturerCollection = ProductManufacturerDBManager.GetProductManufacturersByProductID(ProductID, showHidden);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productManufacturerCollection);
            }
            return productManufacturerCollection;
        }

        /// <summary>
        /// Gets a product manufacturer mapping 
        /// </summary>
        /// <param name="ProductManufacturerID">Product manufacturer mapping identifier</param>
        /// <returns>Product manufacturer mapping</returns>
        public static ProductManufacturer GetByProductManufacturerID(int ProductManufacturerID)
        {
            string key = string.Format(PRODUCTMANUFACTURERS_BY_ID_KEY, ProductManufacturerID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (ProductManufacturer)obj2;
            }

            ProductManufacturer productManufacturer = ProductManufacturerDBManager.GetByProductManufacturerID(ProductManufacturerID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, productManufacturer);
            }
            return productManufacturer;
        }

        /// <summary>
        /// Inserts a product manufacturer mapping
        /// </summary>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="ManufacturerID">Manufacturer identifier</param>
        /// <param name="IsFeaturedProduct">A value indicating whether the product is featured</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product manufacturer mapping </returns>
        public static ProductManufacturer InsertProductManufacturer(int ProductID, int ManufacturerID,
           bool IsFeaturedProduct, int DisplayOrder)
        {
            ProductManufacturer productManufacturer = ProductManufacturerDBManager.InsertProductManufacturer(ProductID, ManufacturerID,
            IsFeaturedProduct, DisplayOrder);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY);
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY);
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_BY_ID_KEY);
            }

            return productManufacturer;
        }

        /// <summary>
        /// Updates the product manufacturer mapping
        /// </summary>
        /// <param name="ProductManufacturerID">Product manufacturer mapping identifier</param>
        /// <param name="ProductID">Product identifier</param>
        /// <param name="ManufacturerID">Manufacturer identifier</param>
        /// <param name="IsFeaturedProduct">A value indicating whether the product is featured</param>
        /// <param name="DisplayOrder">The display order</param>
        /// <returns>Product manufacturer mapping </returns>
        public static ProductManufacturer UpdateProductManufacturer(int ProductManufacturerID, int ProductID, int ManufacturerID,
           bool IsFeaturedProduct, int DisplayOrder)
        {
            ProductManufacturer productManufacturer = ProductManufacturerDBManager.UpdateProductManufacturer(ProductManufacturerID, ProductID, ManufacturerID,
            IsFeaturedProduct, DisplayOrder);
            if (NopContext.IsCachable)
            {
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_ALLBYMANUFACTURERID_KEY);
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_ALLBYPRODUCTID_KEY);
                NopCache.RemoveByPattern(PRODUCTMANUFACTURERS_BY_ID_KEY);
            }

            return productManufacturer;
        }
        #endregion
    }
}
